<?php
/**
 * Static content controller.
 *
 * This file will render views from views/pages/
 *
 * CakePHP(tm) : Rapid Development Framework (http://cakephp.org)
 * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 *
 * Licensed under The MIT License
 * For full copyright and license information, please see the LICENSE.txt
 * Redistributions of files must retain the above copyright notice.
 *
 * @copyright     Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org)
 * @link          http://cakephp.org CakePHP(tm) Project
 * @package       app.Controller
 * @since         CakePHP(tm) v 0.2.9
 * @license       http://www.opensource.org/licenses/mit-license.php MIT License
 */
App::uses('AppController', 'Controller');

/**
 * Static content controller
 *
 * Override this controller by placing a copy in controllers directory of an application
 *
 * @package       app.Controller
 * @link http://book.cakephp.org/2.0/en/controllers/pages-controller.html
 */
class PagesController extends AppController
{
    public $helpers = array('Html', 'Form');
    public $components = array(
        'Session',
    );

    /**
     * This controller does not use a model
     *
     * @var array
     */
    public $uses = array(
        'Category',
        'Page',
        'Widget'
    );

    /**
     * Displays a view
     *
     * @param mixed What page to display
     * @return void
     * @throws NotFoundException When the view file could not be found
     *    or MissingViewException in debug mode.
     */
    public function view($name, $idpage)
    {
        if (!$idpage) {
            throw new NotFoundException(__('Invalid page'));
        }
        $page = $this->Page->findById($idpage);
        if (!$page) {
            throw new NotFoundException(__('Invalid page'));
        }

        $blocks = array();
        $widgets = $this->Widget->find('all');

        $grid = json_decode($page['Page']['grid'], true);
        if ($grid != null)
            foreach ($grid as $element) {
                $block = $element;
                $compareFct = $this->create_equals($element['id_widget']);
                $widget = array_filter($widgets, $compareFct);
                if ($widget != null){
                    $widget = array_values($widget)[0]['Widget'];
                    $widgetComponent = ucfirst($widget['name']).'Init';
                    $widgetFullComponent = ucfirst($widget['name']).'Widget.'.$widgetComponent;
                    $this->{$widgetComponent} = $this->Components->load($widgetFullComponent);
                    $widget['value'] = $this->{ucfirst($widget['name']).'Init'}->transformValue($widget['value']);
                    $block['widget'] = $widget;
                    $blocks[] = $block;
                }
            }

        $this->set('id_cat', $page['Category']['id']);
        $this->set('page', $page);
        $this->set('blocks', $blocks);
        $this->layout = false;
    }

    private function create_equals($id = 0)
    {
        return function ($test) use ($id) {
            return $test['Widget']['id'] == $id;
        };
    }

    public function category($name, $idcat, $idsubcat = null)
    {
        $this->set('id_cat', $idcat);
        if (!$idcat) {
            throw new NotFoundException(__('Invalid page'));
        }
        if ($idsubcat == null)
            $page = $this->Page->findByIdCat($idcat);
        else
            $page = $this->Page->findByIdSubcat($idsubcat);

        $this->set('id_sub_cat', $page ? $page['Page']['id_subcat'] : null);
        $this->set('page', $page ? $page : $this->Category->findById($idcat));
        $this->render('view');
    }

    public function subcategory($name, $idcat, $idsubcat)
    {
        $this->set('id_sub_cat', $idsubcat);
        if (!$idcat) {
            throw new NotFoundException(__('Invalid page'));
        }
        $page = $this->Page->findByIdSubcat($idsubcat);

        $this->set('id_cat', $idcat);
        $this->set('page', $page ? $page : null);
        $this->render('view');
    }

    public function getCats()
    {
        $cats = $this->Category->find('all', array('conditions' => array('Category.id_parent' => null), 'order' => array('Category.id' => 'asc')));
        return $cats;
    }

    public function getSubCats()
    {
        $subcats = $this->Category->findAllByIdParent($this->request->query['idcat'], null, array('Category.id' => 'asc'));
        return $subcats;
    }
}
